package com.cyj.spring.springframeword.beans.factory.support;

import com.cyj.spring.springframeword.beans.BeansException;
import com.cyj.spring.springframeword.beans.factory.BeanFactory;
import com.cyj.spring.springframeword.beans.factory.config.BeanDefinition;

/**
 * @Description: bean定义注册表接口
 * @BelongsProject: small-spring
 * @BelongsPackage: com.cyj.spring.springframeword.beans.factory.support
 * @Author: ChenYongJia
 * @CreateTime: 2021-07-26 14:36
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    /**
     * 获取 bean 注册信息
     * 
     * @param name bean名称
     * @return java.lang.Object
     * @date 2021/7/27 15:29
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public Object getBean(String name) throws BeansException {
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    /**
     * 得到Bean定义
     * 
     * @param beanName bean名称
     * @return com.cyj.spring.springframeword.beans.factory.config.BeanDefinition
     * @date 2021/7/27 15:29
     * @author ChenYongJia
     * @version 1.0
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 创建 bean 容器
     *
     * @param beanName bean名称
     * @param beanDefinition bean定义
     * @return java.lang.Object
     * @date 2021/7/27 15:29
     * @author ChenYongJia
     * @version 1.0
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;

}
